package com.keffect.effects.animatedEffects;

import java.awt.Graphics2D;

import com.keffect.effects.AnimatedEffect;

public class EntryBoucing extends AnimatedEffect {

	private boolean step1, step2, stop;

	private double t;
	private double xi, xf, x;
	private long init;


	public EntryBoucing(long duration) {
		super(duration / 2);
	}


	@Override
	public void initEffectProperties() {
		long frames = (long) ((-1 - Math.sqrt(1 - (4 * -0.01 * getEffectPanel().getWidth()))) / -0.02);
		setFrames(frames);
		xi = getEffectPanel().getWidth();
		xf = 0;
		x = xi;

		step1 = true;
		step2 = false;
		stop = false;
	}


	@Override
	public void doPaintBefore(Graphics2D g) {
		g.translate(x, 0);
	}


	@Override
	public void doPaintAfter(Graphics2D g) {

	}


	@Override
	public void run() {
		if (step1) x = -1 * 0.01 * Math.pow(t, 2) + t + xi;
		else if (step2 || stop) x = getFrames() * 0.5 * Math.pow(Math.E, -0.003 * t) * Math.abs(Math.sin(0.01 * t));

		t++;
	}


	@Override
	public boolean isRunning() {
		if (step1) {
			if (x < xf) {
				step1 = false;
				step2 = true;
				x = 0;
				t = 0;
				init = System.currentTimeMillis();
				return true;
			} else return true;
		} else if (step2) {
			if (System.currentTimeMillis() - init > duration) {
				step2 = false;
				stop = true;
				return true;
			} else return true;
		} else if (stop) {
			if (x < 1.0) return false;
			else return true;

		}

		return true;
	}

}
